package com.xktpx.service.impl;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.xktpx.common.utils.PageUtils;
import com.xktpx.dao.SaleOrderDao;
import com.xktpx.entity.BuyOrderEntity;
import com.xktpx.entity.OrderGoodsDetailEntity;
import com.xktpx.entity.SaleOrderEntity;
import com.xktpx.entity.model.OrderGoods;
import com.xktpx.entity.model.SaleOrderQueryDto;
import com.xktpx.entity.model.SellOrder;
import com.xktpx.service.BuyOrderService;
import com.xktpx.service.OrderGoodsDetailService;
import com.xktpx.service.SaleOrderService;
import com.xktpx.utils.CommonUtils;
import com.xktpx.utils.Query;
import org.dozer.Mapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.*;


@Service("saleOrderService")
public class SaleOrderServiceImpl extends ServiceImpl<SaleOrderDao, SaleOrderEntity> implements SaleOrderService {


    @Autowired
    private BuyOrderService buyOrderService;
    @Autowired
    private OrderGoodsDetailService orderGoodsDetailService;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        Page<SaleOrderEntity> page = this.selectPage(
                new Query<SaleOrderEntity>(params).getPage(),
                new EntityWrapper<SaleOrderEntity>()
        );

        return new PageUtils(page);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void createSaleOrder(SellOrder sellOrder) {
        SaleOrderEntity saleOrderEntity = new SaleOrderEntity();
        BeanUtils.copyProperties(sellOrder,saleOrderEntity);
        saleOrderEntity.setOrderNo(CommonUtils.getOrderIdByTime());
        saleOrderEntity.setCreateTime(new Date());
        if (sellOrder.getSaleType().equals(1) || sellOrder.getSaleType().equals(2)){
            saleOrderEntity.setOrderStatus(-1);
        }else if (sellOrder.getSaleType().equals(1)){
            //修改求购最高求购价订单的状态为待收费状态
            BuyOrderEntity buyOrderEntity = new BuyOrderEntity();
            //待付款状态
            buyOrderEntity.setOrderStatus(1);
            buyOrderEntity.setId(sellOrder.getBuyOrderId());
            buyOrderService.updateById(buyOrderEntity);
        }
        this.insert(saleOrderEntity);

        //订单商品详情
        OrderGoodsDetailEntity orderGoodsDetailEntity = new OrderGoodsDetailEntity();
        orderGoodsDetailEntity.setCreatedTime(new Date());
        orderGoodsDetailEntity.setDesc(sellOrder.getDesc());
        orderGoodsDetailEntity.setGoodsId(sellOrder.getGoodsId().longValue());
        orderGoodsDetailEntity.setGoodsName(sellOrder.getGoodsName());
        orderGoodsDetailEntity.setGoodsPrice(sellOrder.getGoodsPrice());
        orderGoodsDetailEntity.setOrderId(saleOrderEntity.getId());
        orderGoodsDetailEntity.setOrderType(1);
        orderGoodsDetailService.insert(orderGoodsDetailEntity);

    }

    @Override
    public List<SaleOrderQueryDto> purchaseList(Integer goodsId) {
        List<SaleOrderQueryDto> saleOrderQueryDtos = baseMapper.purchaseList(goodsId);
        if (CollectionUtils.isEmpty(saleOrderQueryDtos)){
            return Collections.EMPTY_LIST;
        }
        return saleOrderQueryDtos;
    }

	/*
	 * (non-Javadoc)
	 *
	 * @see com.xktpx.service.SaleOrderService#queryMySale(java.lang.String,
	 * java.lang.String, java.lang.Long)
	 */
	@Override
	public Page<SaleOrderEntity> queryMySale(String pageNo, String pageSize, Long userId) {

		Map<String, Object> params = new HashMap<>();
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		return this.selectPage(new Query<SaleOrderEntity>(params).getPage(),
				new EntityWrapper<SaleOrderEntity>().eq("sale_user_id", userId));
	}

	/*
	 * (non-Javadoc)
	 *
	 * @see com.xktpx.service.SaleOrderService#queryMyShipment(java.lang.String,
	 * java.lang.String, java.lang.Long)
	 */
	@Override
	public Page<SaleOrderEntity> queryMyShipment(String pageNo, String pageSize, Long userId, Integer orderStatus) {
		Map<String, Object> params = new HashMap<>();
		params.put("pageNo", pageNo);
		params.put("pageSize", pageSize);
		return this.selectPage(new Query<SaleOrderEntity>(params).getPage(),
				new EntityWrapper<SaleOrderEntity>().eq("sale_user_id", userId).eq("order_status", 3));
	}

    @Override
    public Page<SaleOrderEntity> queryMyReceiving(String pageNo, String pageSize, Long userId, Integer orderStatus) {
        Map<String, Object> params = new HashMap<>();
        params.put("pageNo", pageNo);
        params.put("pageSize", pageSize);
        return this.selectPage(new Query<SaleOrderEntity>(params).getPage(),
                new EntityWrapper<SaleOrderEntity>().eq("buy_user_id", userId).eq("order_status", orderStatus));
    }

    @Override
    public Page<OrderGoods> queryListByGoodsId(Integer goodsId, Integer sizeId, Integer pageNo, Integer pageSize) {
        Page<OrderGoods> page = new Page<>(pageNo, pageSize);

        return page.setRecords(this.baseMapper.queryListByGoodsId(page, goodsId, sizeId));

    }

    @Override
    public Page<OrderGoods> queryListAllByGoodsId(Integer goodsId, Integer sizeId, Integer pageNo, Integer pageSize) {
        Page<OrderGoods> page = new Page<>(pageNo, pageSize);

        return page.setRecords(this.baseMapper.queryListAllByGoodsId(page, goodsId, sizeId));
    }
}
